package com.qdaily.ui.ui.fragment;

import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.qdaily.supportlibrary.util.QLog;
import com.qdaily.ui.QDBaseFragment;
import com.qdaily.ui.R;
import com.qdaily.ui.data.FragPagerAdapter;
import com.qdaily.ui.net.QDEnum;
import com.qdaily.ui.ui.anim.AlphaTransformer;
import com.qdaily.ui.ui.views.PagerSlidingTabStrip;
import com.qdaily.ui.util.BusProvider;
import com.squareup.otto.Subscribe;

/**
 * Created with IntelliJ IDEA.
 * User: Felix
 * Date: 4/7/15
 * Time: 13:37
 * QQ:74104
 * Email:lovejiuwei@gmail.com
 */
public class MainFragment extends QDBaseFragment implements ViewPager.OnPageChangeListener, View.OnClickListener {
    private ImageView imageViewFragmentMainFirstLabel;
    private ImageView imageViewFragmentMainSecondLabel;
    private ViewPager viewPagerMainFragment;
    private PagerSlidingTabStrip pagerSlidingTabStrip;

    private final int FIRST_LABEL_INDEX = 0;
    private final int SECOND_LABEL_INDEX = FIRST_LABEL_INDEX + 1;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_main;
    }

    @Override
    protected void setupViews(View view) {
        pagerSlidingTabStrip = (PagerSlidingTabStrip) view.findViewById(R.id.pagerSlidingTabStrip);
        imageViewFragmentMainFirstLabel = (ImageView) view.findViewById(R.id.imageViewFragmentMainFirstLabel);
        imageViewFragmentMainFirstLabel.setOnClickListener(this);
        imageViewFragmentMainSecondLabel = (ImageView) view.findViewById(R.id.imageViewFragmentMainSecondLabel);
        imageViewFragmentMainSecondLabel.setOnClickListener(this);
        viewPagerMainFragment = (ViewPager) view.findViewById(R.id.viewPagerMainFragment);
        pagerSlidingTabStrip.setOnPageChangeListener(this);
        imageViewFragmentMainFirstLabel.setPressed(true);
    }

    @Override
    protected void setupData() {
        CategoryFragment.CategoryDataObj homecategoryDataObj = new CategoryFragment.CategoryDataObj().
                setCategoryFragmetType(QDEnum.CategoryFragmentEnum.HOME)
                .setCategoryId(0);
        CategoryFragment homeFragment =  new CategoryFragment();
        homeFragment.onEnter(homecategoryDataObj);

        CategoryFragment.CategoryDataObj sencondcategoryDataObj = new CategoryFragment.CategoryDataObj().
                setCategoryFragmetType(QDEnum.CategoryFragmentEnum.PAPER)
                .setCategoryId(0);
        CategoryFragment sencondFragment =  new CategoryFragment();
        sencondFragment.onEnter(sencondcategoryDataObj);

        viewPagerMainFragment.setAdapter(
                new FragPagerAdapter(getContext().getSupportFragmentManager(),
                        new QDBaseFragment[]{
                                homeFragment,
                                sencondFragment}));

        pagerSlidingTabStrip.setViewPager(viewPagerMainFragment);
        viewPagerMainFragment.setPageTransformer(true, new AlphaTransformer());
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        QLog.w(QDTAG, "onPageSelected");
        BusProvider.getInstance().post(new MainFragmentChange(position == FIRST_LABEL_INDEX? 0 : 1));
        imageViewFragmentMainFirstLabel.setPressed(position == FIRST_LABEL_INDEX);
        imageViewFragmentMainSecondLabel.setPressed(position == SECOND_LABEL_INDEX);
    }

    @Override
    public void onPageScrollStateChanged(int stated) {

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden == false)
        {
            BusProvider.getInstance().post(new MainFragmentChange(viewPagerMainFragment.getCurrentItem() == FIRST_LABEL_INDEX? 0 : 1));
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.imageViewFragmentMainFirstLabel:
                viewPagerMainFragment.setCurrentItem(FIRST_LABEL_INDEX);
                break;
            case R.id.imageViewFragmentMainSecondLabel:
                viewPagerMainFragment.setCurrentItem(SECOND_LABEL_INDEX);
                break;
        }
    }

    @Subscribe
    public void displayFirstLabel(DisplayFirstLabel DISPLAY_FIRST_LABEL) {
        viewPagerMainFragment.setCurrentItem(FIRST_LABEL_INDEX);
    }

    @Subscribe
    public void displaySecondLabel(DisplaySecondLabel DISPLAY_SECOND_LABEL) {
        viewPagerMainFragment.setCurrentItem(SECOND_LABEL_INDEX);
    }

    //这么做就是为了解耦，没什么可说的。
    public static class DisplayFirstLabel {
    }

    public static class DisplaySecondLabel {
    }

    public class MainFragmentChange {
        private int i;

        public MainFragmentChange( int i) {
            this.i = i;
        }

        public int getI() {
            return i;
        }

        public void setI(int i) {
            this.i = i;
        }
    }
}
